package com.xiefengf.rabbitmq.producer.service;

import cn.hutool.json.JSONUtil;
import com.xiefengf.rabbitmq.producer.utils.StringUtils;
import org.springframework.amqp.rabbit.core.BatchingRabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author xiefengf
 * @version 1.0
 * @date 2024/2/3 21:31
 *
 * 批量发送消息类
 */
@Service
public class BatchProductService {

    @Resource(name = "BatchingRabbitTemplate")
    private BatchingRabbitTemplate batchingRabbitTemplate;

    /**
     * 批量发送消息
     * @param userId
     * @param productId
     * @param msg
     */
    public void syncSend1(Integer userId,Integer productId,String msg){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("productId",productId);
        map.put("batch_msg",msg);
        batchingRabbitTemplate.convertAndSend(StringUtils.DIRECT_BATCH_EXCHANGE_NAME,StringUtils.DIRECT_BATCH_KEY, JSONUtil.toJsonStr(map));
    }


    @Resource(name = "RabbitTemplate")
    private RabbitTemplate rabbitTemplate;

    /**
     * 普通发送消息 （测试批量消费消息）
     * @param userId
     * @param productId
     * @param msg
     */
    public void syncSend2(Integer userId,Integer productId,String msg){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("productId",productId);
        map.put("msg",msg);
        rabbitTemplate.convertAndSend(StringUtils.DIRECT_BATCH_EXCHANGE_NAME,StringUtils.DIRECT_BATCH_KEY, JSONUtil.toJsonStr(map));
    }


}
